/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.app.command;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Preconditions;
import com.itgrail.pilot.app.command.cmo.DeleteUserOkrAlignCmd;
import com.itgrail.pilot.domain.user.entity.ObjectiveE;
import com.itgrail.pilot.domain.user.entity.UserOkrAlignE;
import com.itgrail.pilot.domain.user.factory.ObjectiveFactory;
import com.itgrail.pilot.domain.user.factory.UserOkrAlignFactory;
import com.itgrail.pilot.domain.user.ports.ObjectivePort;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.ObjectiveMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.UserOkrAlignMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.ObjectiveDO;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.UserOkrAlignDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.itgrail.grail.cqrs.annotation.CmdHandler;
import com.itgrail.grail.cqrs.command.CommandExecutorI;
import com.itgrail.grail.dto.ResultData;


@CmdHandler
@Slf4j
public class DeleteUserOkrAlignCmdExe implements CommandExecutorI<ResultData<Integer>, DeleteUserOkrAlignCmd> {

    @Autowired
    private UserOkrAlignMapper userOkrAlignMapper;

    @Autowired
    private UserOkrAlignFactory factory;

    @Autowired
    private ObjectiveMapper objectiveMapper;

    @Autowired
    private ObjectivePort objectivePort;

    @Autowired
    private ObjectiveFactory objectiveFactory;

    @Override
    public ResultData execute(DeleteUserOkrAlignCmd cmd) {
        ResultData resultData = ResultData.success(null);
        check(cmd);
        UserOkrAlignE delete = delete(cmd);
        if (null != delete && null != delete.getId()) {
            delete.delete();
            //更新objective对齐人员数量
            updateAlignCount(cmd.getAcceptObjectiveId());
            resultData.setData(true);
        }
        return resultData;
    }

    protected boolean check(DeleteUserOkrAlignCmd cmd) {
        Preconditions.checkNotNull(cmd, "cmd不能为null");
        Preconditions.checkNotNull(cmd.getAcceptObjectiveId(), "被对齐okrId不能为null");
        Preconditions.checkNotNull(cmd.getStartObjectiveId(), "对齐okrId不能为null");
        Preconditions.checkNotNull(cmd.getOkrPeriod(), "对齐okrId不能为null");
        return true;
    }

    protected UserOkrAlignE delete(DeleteUserOkrAlignCmd cmd) {
        QueryWrapper<UserOkrAlignDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("start_objective_id", cmd.getStartObjectiveId());
        queryWrapper.eq("accept_objective_id", cmd.getAcceptObjectiveId());
        queryWrapper.eq("okr_period", cmd.getOkrPeriod());
        UserOkrAlignDO userOkrAlignDO = userOkrAlignMapper.selectOne(queryWrapper);
        if (null != userOkrAlignDO) {
            UserOkrAlignE userOkrAlignE = factory.createUserOkrAlignE();
            userOkrAlignE.setId(userOkrAlignDO.getId());
            return userOkrAlignE;
        }
        log.warn("取消对齐失败:{}", JSON.toJSONString(cmd));
        return null;
    }

    protected void updateAlignCount(Integer id) {
        ObjectiveDO objectiveDO = objectiveMapper.selectById(id);
        ObjectiveE objectiveE = objectiveFactory.createObjectiveE();
        objectiveE.setId(objectiveDO.getId());
        objectiveE.setAlignCount(objectiveDO.getAlignCount() + 1);
        objectivePort.update(objectiveE);
    }

}
